package com.fenghongzhang.broadcasttest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {
    private TextView tValues;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        tValues = (TextView) findViewById(R.id.t_values);


        MyReceiver myReceiver = new MyReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.feng");
        registerReceiver(myReceiver,intentFilter);

//        tValues.setText(?);

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);

    }


    //内部的广播接收者

    /**
     * 1,清单. -->添加过滤器,频道,
     * 2,动态注册 -> 代码中注册频道
     */
    public class MyReceiver extends BroadcastReceiver{

        public MyReceiver(){
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(action.equals("com.feng")){
                Bundle extras = intent.getExtras();
                String name = extras.getString("name");
                tValues.setText(name);
            }
        }
    }


}
